package org.example.project_12.service.impl;

import org.example.project_12.entity.Comment;
import org.example.project_12.mapper.CommentMapper;
import org.example.project_12.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public Comment addComment(Long docId, Long authorId, String content, Long parentId) {
        Comment comment = new Comment();
        comment.setDocId(docId);
        comment.setAuthorId(authorId);
        comment.setContent(content);
        comment.setParentId(parentId);
        comment.setCreatedAt(LocalDateTime.now());
        commentMapper.insert(comment);
        return comment;
    }

    @Override
    public List<Comment> getCommentsByDoc(Long docId) {
        return commentMapper.selectByDocId(docId);
    }

    @Override
    public Comment getCommentById(Long commentId) {
        return commentMapper.selectById(commentId);
    }

    @Override
    public void deleteComment(Long commentId, Long userId, boolean isAdmin) {
        Comment comment = commentMapper.selectById(commentId);
        if (comment == null) throw new RuntimeException("评论不存在");
        if (isAdmin || comment.getAuthorId().equals(userId)) {
            commentMapper.deleteById(commentId);
        } else {
            throw new RuntimeException("无权限删除");
        }
    }

    @Override
    public List<Comment> getCommentsByUser(Long userId) {
        return commentMapper.selectByUserId(userId);
    }
} 